package redis

import (
	"cart_srv/pb/cart"
	"context"
	"fmt"
	"strconv"
)

func AddCart(userId int64, goodsId int64, cart map[string]interface{}) error {
	key := fmt.Sprintf("cart:%d:%d", userId, goodsId)
	return Rdb.HMSet(context.Background(), key, cart).Err()
}
func GetExist(userId int64, goodsId int64) bool {
	key := fmt.Sprintf("cart:%d:%d", userId, goodsId)
	return Rdb.Exists(context.Background(), key).Val() == 1
}

func UpdateCart(userId int64, goodsId int64, num int64) error {
	key := fmt.Sprintf("cart:%d:%d", userId, goodsId)
	return Rdb.HIncrBy(context.Background(), key, "num", num).Err()
}
func DelCart(userId int64, goodsId int64) error {
	key := fmt.Sprintf("cart:%d:%d", userId, goodsId)
	return Rdb.Del(context.Background(), key).Err()
}
func ClearCart(userId int64) {
	key := fmt.Sprintf("cart:%d:*", userId)
	keys := Rdb.Keys(context.Background(), key).Val()
	for _, v := range keys {
		Rdb.Del(context.Background(), v)
	}
}
func ListCart(userId int64) (cartList []*cart.CartList, err error) {
	key := fmt.Sprintf("cart:%d:*", userId)
	keys := Rdb.Keys(context.Background(), key).Val()
	for _, v := range keys {
		GoodsMap := Rdb.HGetAll(context.Background(), v).Val()
		userid, _ := strconv.Atoi(GoodsMap["user_id"])
		goodsId, _ := strconv.Atoi(GoodsMap["goods_id"])
		num, _ := strconv.Atoi(GoodsMap["num"])
		goodsPrice, _ := strconv.ParseFloat(GoodsMap["goods_price"], 64)
		checked, _ := strconv.Atoi(GoodsMap["checked"])

		cartList = append(cartList, &cart.CartList{
			UserId:     int64(userid),
			GoodsId:    int64(goodsId),
			Num:        int64(num),
			GoodsTitle: GoodsMap["goods_title"],
			GoodsCover: GoodsMap["goods_cover"],
			GoodsPrice: float32(goodsPrice),
			Checked:    int64(checked),
		})

	}
	return
}
func ListCartOne(userId int64, goodsId int64) (cartList []*cart.CartListOne, err error) {
	key := fmt.Sprintf("cart:%d:%d", userId, goodsId)
	GoodsMap := Rdb.HGetAll(context.Background(), key).Val()
	userid, _ := strconv.Atoi(GoodsMap["user_id"])
	goods_id, _ := strconv.Atoi(GoodsMap["goods_id"])
	num, _ := strconv.Atoi(GoodsMap["num"])
	goodsPrice, _ := strconv.ParseFloat(GoodsMap["goods_price"], 64)
	checked, _ := strconv.Atoi(GoodsMap["checked"])

	cartList = append(cartList, &cart.CartListOne{
		UserId:     int64(userid),
		GoodsId:    int64(goods_id),
		Num:        int64(num),
		GoodsTitle: GoodsMap["goods_title"],
		GoodsCover: GoodsMap["goods_cover"],
		GoodsPrice: float32(goodsPrice),
		Checked:    int64(checked),
	})

	return
}
func CartCount(userId int64) (count int64) {
	key := fmt.Sprintf("cart:%d:*", userId)
	keys := Rdb.Keys(context.Background(), key).Val()
	for _, v := range keys {
		fmt.Println(v)
		GoodsMap := Rdb.HGetAll(context.Background(), v).Val()
		num, _ := strconv.Atoi(GoodsMap["num"])
		count += int64(num)
	}
	return count
}
func CartAmount(userId int64) (amount float64) {
	key := fmt.Sprintf("cart:%d:*", userId)
	keys := Rdb.Keys(context.Background(), key).Val()
	for _, v := range keys {
		fmt.Println(v)
		GoodsMap := Rdb.HGetAll(context.Background(), v).Val()
		num, _ := strconv.Atoi(GoodsMap["num"])
		goodsPrice, _ := strconv.ParseFloat(GoodsMap["goods_price"], 64)
		amount += goodsPrice * float64(num)
	}
	return amount
}
func CheckCart(userId int64, checked int64) {
	key := fmt.Sprintf("cart:%d:*", userId)
	keys := Rdb.Keys(context.Background(), key).Val()
	for _, v := range keys {
		Rdb.HSet(context.Background(), v, "checked", checked)
	}
}
